<?php
/*
Template Name: json
*/
?>
<?php
//接收http参数，传递用于下一步获取筛选文章信息
$nav = intval($_GET['navn'])>0?intval($_GET['navn']):1;
$perpage = intval($_GET['per'])?intval($_GET['per']):30;
$cate = intval($_GET['catn'])?intval($_GET['catn']):6;
$offset = ($nav - 1) * $perpage;
$games = get_posts(array(
    "category" => $cate,
    "numberposts" => $perpage,
    "offset" => $offset
  ));
?>
<?php
//获取文章信息，输出json
$jsonpost = array();
if( $games ) :
foreach( $games as $post ) : setup_postdata( $post );
        // construct our array for json
        // apply_filters to content to process shortcodes, etc
        $jsonpost["id"] = get_the_ID();
        $jsonpost["title"] = get_the_title();
        $jsonpost["url"] = apply_filters('the_permalink', get_permalink());
        $jsonpost["cat"] = get_all_category();
        // $jsonpost["content"] = apply_filters('the_content', get_the_content());
        $jsonpost["thumbnail_image"] = src_post_thumbnail();

        //$jsonpost["content"] = get_the_content();

        // would rather do iso 8601, but not supported in gwt (yet)
        //$jsonpost["date"] = get_the_time('d F Y');

        $jsonposts[] = $jsonpost; 
endforeach;
endif;
?>

<?php
//获取当前分类、当前页数、总页数，输出json
$jsoninfo = array();
if($cate == 6){
	$jsoninfo["totalpage"] = ceil(gamux_get_allfirst_posts(6)/$perpage);
}
else {
	$jsoninfo["totalpage"] = ceil((get_category($cate)->count)/$perpage);
}
?>

<?php
$outputJSON = array(
    "newgames" => $jsonposts,
	"info" => $jsoninfo
);
echo json_encode($outputJSON);
?>
